﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangeDealer.aspx.cs" Inherits="H2.NET.Web.Pages.SMD.ChangeDealer" %>
<%@ Register src="../../Controls/DwzAssessment.ascx" tagname="DwzAssessment" tagprefix="uc" %>
<%@ Register src="../../Controls/DwzAssessmentList.ascx" tagname="DwzAssessmentList" tagprefix="uc" %>
<%@ Register src="../../Controls/AjaxUpload.ascx" tagname="ajaxUpload" tagprefix="uc" %>
<%@ Register src="uc_change_field.ascx" tagname="uc_change_field" tagprefix="uc" %>
<%@ Register src="uc_dealer.ascx" tagname="uc_dealer" tagprefix="uc" %>
<%@ Register src="uc_head.ascx" tagname="uc_head" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />
<head id="Head1" runat="server">
    <title>经销商变更</title>
    <link href="<%=Page.ResolveUrl("~/Resource/Css/Style.css")%>" type="text/css" rel="stylesheet" />
    <link href="<%=Page.ResolveUrl("~/Resource/jBox/Blue/jbox.css")%>" rel="stylesheet" type="text/css" />
    <link href="<%=Page.ResolveUrl("~/Resource/Css/validform.css")%>" rel="stylesheet" type="text/css" />
    <link href="<%=Page.ResolveUrl("~/Resource/qtip/jquery.qtip.min.css")%>" rel="stylesheet" type="text/css" />
    <script src="<%=Page.ResolveUrl("~/Resource/dwz/js/jquery-1.7.2.min.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/Js/Common.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/Js/jquery.money.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/jBox/jquery.jBox-2.3.min.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/jBox/jquery.jBox-zh-CN.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/Js/Validform_v5.3.2.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/My97DatePicker/WdatePicker.js")%>" type="text/javascript"></script>
    <script src="<%=Page.ResolveUrl("~/Resource/qtip/jquery.qtip.min.js")%>" type="text/javascript"></script>
    <!--[if lt IE 8 ]><script src="<%=Page.ResolveUrl("~/Resource/Js/json2.min.js")%>" type="text/javascript"></script><![endif]-->
    <style type="text/css">
        #store_list input[type=text] { width:95%; }
    </style>
    <script type="text/javascript"> 
        var amount = 0;
        $(function () {
            /* 初始化相关样式、表单验证提示 st */
            $("[datatype='*']").after("<em class='required'>*</em>");
            $("#form1").Validform({
                tipSweep: true,
                tiptype: function (msg, o, cssctl) {
                    if (o.type == 3) {
                        $.jBox.tip(msg, 'info');
                    }
                }
            });
            /* 初始化相关样式、表单验证提示 ed */

            // 双击选择门店之后跳转的url
            $("#return_url").val('<%=Page.ResolveUrl("~/pages/smd/ChangeDealer.aspx?TaskID=" + TaskAPI.TaskID)%>');

            /* 初始化控件提示 st */
            $.ajax({
                type: 'POST',
                url: '<%=Page.ResolveUrl("~/pages/smd/ajaxtools/handler.ashx?action=get_tips")%>',
                data: { pagename: 'changedealer.aspx' },
                dataType: "json",
                cache: false,
                success: function (json) {
                    for (var o in json) {
                        var ControlID = json[o].ControlID;
                        var Content = json[o].Content;
                        $("#" + ControlID).qtip({
                            content: Content
                        });
                    }
                }
            });
            /* 初始化控件提示 ed */
            
            /* 选择门店 st */
            $(":checkbox[name='cb_selected']").click(function () {
                $(":checkbox.cb_item").attr("checked", $(this).attr("checked") == "checked");
            });
            /* 选择门店 ed */
        });

        /* 打开选择门店窗体 st */
        function openStoreDialog(obj) {
            if ($(obj).hasClass("search")) {
                $.jBox("id:search_store", {
                    title: "双击选择门店",
                    width: 650,
                    height: 500,
                    buttons: {}
                });
            }
        }
        /* 打开选择门店窗体 ed */

        /* 提交表单 st */
        function confirmSubmit() {
            if (window.confirm("确认要提交吗？")) {

                // 数据验证
                var old_id = $("#uc_dealer_old_dealer_id").val();
                var new_id = $("#uc_dealer_new_dealer_id").val();
                var store_list = $("#store_list").html();
                if ("<%:step%>" === "Submit Data") {
                    
                    if ($(".cb_item").eq(0).attr("disabled") !== "disabled") {
                        if (new_id == old_id) {
                            $.jBox.tip("原经销商和新经销商不能相同", 'info');
                            return false;
                        }

                        if (store_list == "") {
                            $.jBox.tip("该经销商没有门店", 'info');
                            return false;
                        }
                        
                    } else {
                        if ($("#device_list").children().size() > 0 && $("#device_list").children().first().children().last().text() !== "暂无数据") {
                            $("#device_list").children().each(function() {
                                amount += parseInt($(this).children().last().text().replaceAll(',', ''));
                            });
                        }
                    } 
                    if (amount > 0 && $("#cb_depositchange").attr("checked") !== "checked") {
                        $.jBox.tip("检测到设备有保证金，请勾选发起保证金", 'info');
                        return false;
                    }
                    if(amount  === 0){
                        if (window.confirm("检测到保证金为" + amount + "请确保是否有进款，如有需要发起保证金变更,谢谢!")) {
                            $.jBox.tip('正在处理，请稍等...', 'loading');
                            return true;
                        } else {
                            return false;
                        }
                    }
                }

                
                $.jBox.tip('正在处理，请稍等...', 'loading');
                return true;
            }
            return false;
        }
        /* 提交表单 ed */

        /* 地区级联 st */
        function initProvince(arg) {
            $.jBox.closeTip();
            var val = $(arg).val();
            var id = $(arg).attr("id");
            if ($.trim(val)) {
                $.ajax({
                    type: 'POST',
                    url: '<%=Page.ResolveUrl("~/pages/smd/store.aspx?action=initProvince")%>',
                    data: { ar: val, type: (id == "ddl_province" ? "province" : "city") },
                    dataType: "json",
                    cache: false,
                    success: function (json) {
                        var rst = json.navTabId.split("|");
                        var html = "";
                        for (var i = 0; i < rst.length; i++) {
                            if (rst[i]) {
                                var list = rst[i].split(",");
                                html += "<option value='" + list[0] + "'>" + list[1] + "</option>";
                            }
                        }
                        if (id == "ddl_province") {
                            $("#ddl_cityName").find("option:gt(0)").remove().end().append(html);

                        }
                        else {
                            $("#ddl_province").find("option:gt(0)").remove().end().append(html);
                            $("#ddl_cityName").find("option:gt(0)").remove();
                        }
                    }
                });
            }
            else {
                if (id == "ddl_province") {
                    $("#ddl_cityName").find("option:gt(0)").remove();

                }
                else {
                    $("#ddl_province").find("option:gt(0)").remove();
                    $("#ddl_cityName").find("option:gt(0)").remove();
                }
            }
        }
        /* 地区级联 ed */
        $(function () {
            $("[name=IsUpgraded]").click(function () {
                IsUpgradedFunction(this);
            });

            $("[name=IsDowngraded]").click(function () {
                IsDowngradedFunction(this);
            });

            $("[name=IsRenew]").click(function () {
                isRenewFunction(this);
            });

            $("[name=IsUpgraded]").each(function () {
                if ($(this).attr("checked") == "checked") {
                    isRenewFunction(this);
                }
            });

            $("[name=IsDowngraded]").each(function () {
                if ($(this).attr("checked") == "checked") {
                    isRenewFunction(this);
                }
            });

            $("[name=IsRenew]").each(function () {
                if ($(this).attr("checked") == "checked") {
                    isRenewFunction(this);
                }
            });
        });

        function IsUpgradedFunction(obj) {
            if ($(obj).val() == "true") {
                $("#BeforeUpgradedType").parent().show();
                $("#BeforeUpgradedType").attr("datatype", "*").after("<em class='required'>*</em>");
            } else {
                $("#BeforeUpgradedType").parent().hide();
                $("#BeforeUpgradedType").attr("datatype", "").parent().find("em").remove();
            }
        }

        function IsDowngradedFunction(obj) {
            if ($(obj).val() == "true") {
                $("#BeforeDowngradedType").parent().show();
                $("#BeforeDowngradedType").attr("datatype", "*").after("<em class='required'>*</em>");
            } else {
                $("#BeforeDowngradedType").parent().hide();
                $("#BeforeDowngradedType").attr("datatype", "").parent().find("em").remove();
            }
        }


        function isRenewFunction(obj) {
            if ($(obj).val() == "true") {
                $("#BeforeRenewType").parent().show();
                $("#BeforeRenewType").attr("datatype", "*").after("<em class='required'>*</em>");
                $("#tb_LastRenewDate").parent().show();
                $("#tb_LastRenewDate").attr("datatype", "*").after("<em class='required'>*</em>");
            } else {
                $("#BeforeRenewType").parent().hide();
                $("#BeforeRenewType").attr("datatype", "").parent().find("em").remove();
                $("#tb_LastRenewDate").parent().hide();
                $("#tb_LastRenewDate").attr("datatype", "").parent().find("em").remove();
            }
        }

        

        function checkStore(obj) {
            var authid = $(obj).attr("authid");
            $(".authid").each(function() {
                if ($(this).attr("authid") === authid) {
                    if (obj.checked) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                }
            });
            checkAmount();
        }


        function CheckAllStore(checked) {
            $(".authid").each(function () {
                if (checked) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
            checkAmount();
        }

        function checkAmount() {
            amount = 0;
            $(".authid:visible").each(function() {
                amount += parseInt($(this).children().last().text().replaceAll(',', ''));
            });
            if (amount === 0) {
                $("#cb_depositchange").removeAttr("checked");
            } else {
                $("#cb_depositchange").attr("checked","checked");
            }
        }

        function dealerCallBack(dealerid) {
            $.jBox.tip('正在获取原经销商的门店信息，请稍等...', 'loading');
            $.ajax({
                type: 'POST',
                url: '<%= Page.ResolveUrl("~/Pages/SMD/ajaxtools/handler.ashx?action=get_store_by_dealer")%>',
                data: { dealerid: dealerid },
                dataType: "json",
                cache: false,
                success: function (json) {
                    if (json == "") {
                        $("#store_list_tip").show();
                        $("#store_list").html("");
                    } else {
                        $("#store_list_tip").hide();
                        var i = 0;
                        var tbody = "";
                        for (var o in json) {
                            var tr = "<tr>" +
                                "   <td style='text-align:center;'><input type='checkbox' class='cb_item' authid=" + IsNull(json[o].AuthID) + " name='StoreID" + i + "' value='" + (IsNull(json[o].ID)) + "' checked onclick = checkStore(this) /></td>" +
                                "   <td style='text-align:center;'><input type='hidden' name='ID" + i + "' value='" + (IsNull(json[o].ID)) + "' />" + IsNull(json[o].AuthID) + "</td>" +
                                "   <td style='text-align:center;'>" + (IsNull(json[o].StoreStatus)) + "</td>" +
                                "   <td><input type='text' name='StoreName" + i + "' value='" + (IsNull(json[o].StoreName)) + "' /></td>" +
                                "   <td><input type='text' name='ContractPerson" + i + "' value='" + (IsNull(json[o].ContractPerson)) + "' /></td>" +
                                "   <td><input type='text' name='Telephone" + i + "' value='" + (IsNull(json[o].Telephone)) + "' /></td>" +
                                "   <td><input type='text' readonly name='Address" + i + "' value='" + (IsNull(json[o].Address)) + "' /></td>" +
                                "</tr>";

                            tbody += tr;

                            i++;
                        }
                        $("#hf_store_count").val(i);
                        $("#store_list").html(tbody);
                        
                    }
                    $.ajax({
                        type: 'POST',
                        url: '<%= Page.ResolveUrl("~/Pages/SMD/ajaxtools/handler.ashx?action=get_device_by_dealer")%>',
                        data: { dealerid: dealerid },
                        dataType: "json",
                        cache: false,
                        success: function (json) {
                            if (json == "") {
                                $("#tr_devicedeposit_tip").show();
                                $("#device_list").html("");
                                $("#cb_depositchange").removeAttr("checked");
                                $("#cb_depositchange").attr("disabled", "disabled");
                            } else {
                                $("#cb_depositchange").removeAttr("disabled");
                                $("#tr_devicedeposit_tip").hide();
                                var i = 0;
                                var tbody = "";
                                for (var o in json) {
                                    var tr = "<tr authid=" + json[o].AuthID + " class = 'authid'>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].AuthID) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].EapproveSN) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].Status) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].FullSetSN) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].InstallDate) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].PurchasedDate) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].InstallDate) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].DispenserModel) + "</td>" +
                                            "   <td style='text-align:center;'>" + IsNull(json[o].PcModel) + "</td>" +
                                            "   <td style='text-align:right;'>" + fmoney(IsNull(json[o].Amount)) + "</td>" +
                                            "</tr>";

                                    tbody += tr;

                                    i++;
                                }
                                $("#device_list").html(tbody);
                                checkAmount();
                            }
                            $.jBox.closeTip();
                        }
                    });

                    $.jBox.closeTip();
                    
                }
            });

        }

        function IsNull(val) {
            return val == null ? "" : val;
        }
    </script>
</head>
<body>
    <div class="flowpage">
    <form id="form1" runat="server">
        <asp:HiddenField ID="hf_store_count" runat="server" />
        <!-- 标题 st -->
        <uc1:uc_head ID="uc_head1" runat="server" />
        <!-- 标题 ed -->

        <!-- 门店主数据变更表单 st -->
        <table class="formTable">

            <!-- 原经销商 st -->
            <uc:uc_dealer ID="uc_dealer_old" runat="server" />
            <!-- 原经销商 ed -->

            <tbody>
                <tr><td class="title" colspan="6">门店列表</td></tr>
                <tr>
                    <td colspan="6">
                        <table class="htable">
                            <thead>
                                <tr>
                                    <th style="text-align: center; width:5%;"><asp:CheckBox ID="cb_selected" runat="server" Enabled="false" onclick="CheckAllStore(this.checked)" /></th>
                                    <th style="text-align: center; width:10%;">门店授权号</th>
                                    <th style="text-align: center; width:10%;">门店状态</th>
                                    <th style="text-align: center; width:25%;">门店名称</th>
                                    <th style="text-align: center; width:10%;">联系人</th>
                                    <th style="text-align: center; width:10%;">电话</th>
                                    <th style="text-align: center;">门店地址</th>
                                </tr>
                            </thead>
                            <tbody id="store_list">
                                <asp:Repeater ID="rpt_stores" runat="server" OnItemDataBound="rpt_stores_ItemDataBound">
                                <ItemTemplate>
                                    <tr>
                                        <td style='text-align:center;'>
                                            <input type='hidden' runat="server" id="storeid" name='ID<%#Container.ItemIndex%>' value='<%#Eval("StoreID")%>' />
                                            <input type="checkbox" id="authidCheck" runat="server" name="StoreID<%#Container.ItemIndex%>" class="cb_item"  onclick = "checkStore(this)" />
                                        </td>
                                        <td style='text-align:center;'>
                                            <input type='hidden' runat="server" id="authidcode" name='ID<%#Container.ItemIndex%>' value='<%#Eval("AuthID")%>' /><%#Eval("AuthID")%>
                                        </td>
                                        <td style='text-align:center;'><%#Eval("StoreStatus")%></td>
                                        <td><input type='text' name='StoreName<%#Container.ItemIndex%>' value='<%#Eval("StoreName")%>' /></td>
                                        <td><input type='text' name='ContractPerson<%#Container.ItemIndex%>' value='<%#Eval("ContactPerson")%>' /></td>
                                        <td><input type='text' name='Telephone<%#Container.ItemIndex%>' value='<%#Eval("PhoneNumber")%>' /></td>
                                        <td><input type='text' name='Address<%#Container.ItemIndex%>' value='<%#Eval("StoreAddress")%>' readonly /></td>
                                    </tr>
                                </ItemTemplate>
                                </asp:Repeater>
                                <asp:Repeater ID="rpt_stores_readonly" runat="server">
                                <ItemTemplate>
                                    <tr>
                                        <td style='text-align:center;'><input type="checkbox" name="StoreID<%#Container.ItemIndex%>" value="<%#Eval("StoreID")%>" class="cb_item" checked="checked" disabled /></td>
                                        <td style='text-align:center;'><%#Eval("AuthID")%></td>
                                        <td style='text-align:center;'><%#Eval("StoreStatus")%></td>
                                        <td><%#Eval("StoreName")%></td>
                                        <td style='text-align:center;'><%#Eval("ContactPerson")%></td>
                                        <td style='text-align:center;'><%#Eval("PhoneNumber")%></td>
                                        <td><%#Eval("StoreAddress")%></td>
                                    </tr>
                                </ItemTemplate>
                                </asp:Repeater>
                            </tbody>
                            <tr id="store_list_tip" runat="server">
                                <td colspan="6" style="text-align:center;">暂无数据</td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr><td class="title" colspan="6">设备列表</td></tr>
                <tr>
                    <td colspan="6">
                        <table class="htable">
                            <thead>
                                <tr>
                                    <th style="text-align: center; width:10%;">授权号</th>
                                    <th style="text-align: center; width:10%;">系统流水号</th>
                                    <th style="text-align: center; width:10%;">设备状态</th>
                                    <th style="text-align: center; width:10%;">固定资产号</th>
                                    <th style="text-align: center; width:10%;">机器初购年份</th>
                                    <th style="text-align: center; width:10%;">安装时间</th>
                                    <th style="text-align: center; width:10%;">调色机型号</th>
                                    <th style="text-align: center; width:10%;">混合机型号</th>
                                    <th style="text-align: center; width:10%;">电脑型号</th>
                                    <th style="text-align: center; width:10%;">机器保证金</th>
                                </tr>
                            </thead>
                            <tbody id="device_list">
                            <asp:Repeater ID="rpt_devicedeposit" runat="server" OnItemDataBound="rpt_devicedeposit_ItemDataBound">
                                <ItemTemplate>
                                <tr runat="server" id="deviceTr">
                                    <td style="text-align: center">
                                        <input type='hidden' runat="server" id="d_authid" name='ID<%#Container.ItemIndex%>' value='<%#Eval("AuthID")%>' />
                                        <%#Eval("AuthID") %>
                                    </td>
                                    <td style="text-align: center"><%#Eval("EapproveSN") %></td>
                                    <td style="text-align: center"><%#Eval("Status") %></td>
                                    <td style="text-align: center"><%#Eval("FullSetSN") %></td>
                                    <td style="text-align: center"><%#Eval("PurchasedDate") == null ? "" : Convert.ToDateTime(Eval("PurchasedDate").ToString()).ToString("yyyy-MM") %></td>
                                    <td style="text-align: center"><%#Eval("InstallDate") == null ? "" : Convert.ToDateTime(Eval("InstallDate").ToString()).ToString("yyyy-MM") %></td>
                                    <td style="text-align: center"><%#Eval("DispenserModel") %></td>
                                    <td style="text-align: center"><%#Eval("ShakerModel") %></td>
                                    <td style="text-align: center"><%#Eval("PcModel") %></td>
                                    <td style="text-align: right"><%#H2.NET.Common.Utils.StrToDecimal(Eval("Amount") == null ? "" : Eval("Amount").ToString(), 0).ToString("N")%></td>
                                </tr>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tbody>
                            <tr id="tr_devicedeposit_tip" runat="server">
                                <td colspan="10" style="text-align:center;">暂无数据</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>

            <!-- 经销商信息 st -->
            <uc:uc_dealer ID="uc_dealer_new" runat="server" />
            <!-- 经销商信息 ed -->
            
            <tr><td colspan="6" class="title">其他信息</td></tr>
            <tr>
                <th>保证金变更流程：</th>
                <td colspan="5">
                    <asp:CheckBox ID="cb_depositchange" runat="server" Text="是否需要发起" Checked="true" />
                    <asp:Literal ID="l_depositchange" runat="server"></asp:Literal>
                    <label style="margin-left:30px;color:red">备注：若有设备保证金，需发起保证金变更流程</label>
                </td>
            </tr>
            <!-- 字段变更列表 st -->
            <tbody id="tb_changelogs" runat="server" visible="false">
                <tr>
                    <th>字段变更列表：</th>
                    <td colspan="5"><uc:uc_change_field ID="uc_change_field1" runat="server" /></td>
                </tr>
            </tbody>
            <!-- 字段变更列表 ed -->

            <!-- 审批列表 st -->
            <tbody id="assessment" runat="server" Visible="False">
                <tr>
                    <td class="title" colspan="6">审批列表</td>
                </tr>
                <tr>
                    <td colspan="6">
                        <uc:DwzAssessmentList ID="DwzAssessmentList1" runat="server" />
                    </td>
                </tr>
            </tbody>
            <!-- 审批列表 ed -->

            <!-- 相关附件 st -->
            <tbody>
                <tr>
                    <th>相关附件：</th>
                    <td colspan="5"><uc:ajaxUpload ID="ajaxUpload1" runat="server" /></td>
                </tr>
            </tbody>
            <!-- 相关附件 ed -->
            
            <!-- 审批意见 st -->
            <tbody><uc:DwzAssessment ID="DwzAssessment1" runat="server" /></tbody>
            <!-- 审批意见 ed -->
        </table>
        <!-- 门店主数据变更表单 ed -->

        <!-- 操作按钮 st -->
    <%  if(H2.NET.Common.AksRequest.GetString("showbtn") != "0")
        { %>
        <div style="margin: 5px; padding-bottom: 10px; text-align: center;">
            <asp:Button ID="btn_submit" runat="server" CssClass="u-btn" Text="提交(Submit)" OnClick="btn_submit_Click" OnClientClick="return confirmSubmit();" />
            <input type="button" class="u-btn u-btn-c4" value="关闭(Close)" onclick="custom_close()" />
            <asp:HyperLink ID="hl_ExportPdf" runat="server" Visible="false" Target="_blank" CssClass="u-btn u-btn-c3" style="height:28px; line-height:28px; display:inline-block;">生成(PDF)</asp:HyperLink>
            <asp:Button ID="btn_print" runat="server" CssClass="u-btn u-btn-c2" Text="打印(Print)" OnClientClick="window.print()" Visible="false" />
        </div>
    <%  } %>
        <!-- 操作按钮 ed -->    

    </form>
    </div>
</body>
</html>
